﻿Public Class QuotePost

    Private acct As TumblePower.tumblr.Account
    Private TumblrData As TumblePower.tumblr

    Public Sub New(ByVal account As TumblePower.tumblr.Account)
        InitializeComponent()
        acct = account
    End Sub

    Private Sub CreateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateButton.Click
        CreateButton.Text = "Please Wait..."
        CreateButton.Enabled = False
        CancelButton.Enabled = False

        TumblrData = New TumblePower.tumblr
        Dim t As New TumblePower.tumblr.Quote
        With t
            .Email = acct.Email
            .Password = acct.Password
            .TheQuote = QuoteTextBox.Text
            .Source = SourceTextBox.Text
            .Tags = TagsTextBox.Text
            .IsPrivate = PrivatePostCheckBox.Checked
        End With
        Dim ap As New TumblePower.tumblr.apostQuote(AddressOf TumblrData.postQuote)

        Dim result As IAsyncResult = ap.BeginInvoke(t, New AsyncCallback(AddressOf CallbackMethod), ap)
    End Sub

    Private Sub CallbackMethod(ByVal ar As IAsyncResult)
        If Me.InvokeRequired Then
            Me.BeginInvoke(New AsyncCallback(AddressOf CallbackMethod), ar)
            Exit Sub
        End If

        Dim caller As TumblePower.tumblr.apostQuote = DirectCast(ar.AsyncState, TumblePower.tumblr.apostQuote)
        Dim returnValue As TumblePower.Status = caller.EndInvoke(ar)

        If returnValue.Code <> 201 Then
            MsgBox(returnValue.Msg)
        Else
            MsgBox("Post Created")
            Me.Close()
            QuoteTextBox.Clear()
            SourceTextBox.Clear()
            TagsTextBox.Clear()
        End If
        CreateButton.Text = "Create Post"
        CancelButton.Enabled = True
        CreateButton.Enabled = True
    End Sub

    Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelButton.Click
        If MsgBox("Cancel editing this post? All unpublished changes will be lost.", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Me.Close()
            QuoteTextBox.Clear()
            SourceTextBox.Clear()
            TagsTextBox.Clear()
        End If
    End Sub
    Private Sub BoldToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BoldToolStripMenuItem.Click
        Dim InsertStringBefore As String = "<b>"
        Dim InsertStringAfter As String = "</b>"
        Dim SelStart As Integer = SourceTextBox.SelectionStart
        Dim SelLength As Integer = SourceTextBox.SelectionLength

        SourceTextBox.Text = SourceTextBox.Text.Insert(SelStart + SelLength, InsertStringAfter)
        SourceTextBox.Text = SourceTextBox.Text.Insert(SelStart, InsertStringBefore)
    End Sub

    Private Sub ItalicToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ItalicToolStripMenuItem.Click
        Dim InsertStringBefore As String = "<i>"
        Dim InsertStringAfter As String = "</i>"
        Dim SelStart As Integer = SourceTextBox.SelectionStart
        Dim SelLength As Integer = SourceTextBox.SelectionLength

        SourceTextBox.Text = SourceTextBox.Text.Insert(SelStart + SelLength, InsertStringAfter)
        SourceTextBox.Text = SourceTextBox.Text.Insert(SelStart, InsertStringBefore)
    End Sub

    Private Sub StrikeoutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StrikeoutToolStripMenuItem.Click
        Dim InsertStringBefore As String = "<strike>"
        Dim InsertStringAfter As String = "</strike>"
        Dim SelStart As Integer = SourceTextBox.SelectionStart
        Dim SelLength As Integer = SourceTextBox.SelectionLength

        SourceTextBox.Text = SourceTextBox.Text.Insert(SelStart + SelLength, InsertStringAfter)
        SourceTextBox.Text = SourceTextBox.Text.Insert(SelStart, InsertStringBefore)
    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
        SourceTextBox.Cut()
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        SourceTextBox.Copy()
    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
        SourceTextBox.Paste()
    End Sub
End Class